package com.nahan.secondapplication.ClassScheduleCard;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.nahan.secondapplication.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.zhouchaoyuan.excelpanel.ExcelPanel;

public class ClassScheduleCardActivity extends AppCompatActivity {

    public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
    public static final String WEEK_FORMAT_PATTERN = "EEEE";
    public static final String[] subject = {"数学", "英语", "体育", "美术", "化学", "语文", "物理"};
    public static final String[] teacherNAME = {"刘老师", "迪老师", "柳老师", "范老师", "唐老师", "杨老师", "潘老师"};
    public static final int[] x = {0, 1, 1, 3, 2, 4, 5};
    public static final int[] y = {1, 2, 4, 4, 3, 1, 0};
    public static final String[] left_time = {"08:00\n~\n08:40", "08:50\n~\n09:30", "10:00\n~\n10:40", "10:50\n~\n11:30",
            "13:20\n~\n14:00", "14:10\n~\n14:50", "15:05\n~\n15:45", "15:55\n~\n16:35"};
    public static final String[] top_Week = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
    public static final long ONE_DAY = 24 * 3600 * 1000;
    public static final int PAGE_SIZE = 7;//有多少纵
    public static final int ROW_SIZE = 8;//有多少横

    private ExcelPanel excelPanel;
    private CustomAdapter adapter;
    private List<RowTitle> rowTitles;
    private List<ColTitle> colTitles;
    private List<List<Cell>> cells;
    private List<CourseBean> courseBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_schedule_card);

        excelPanel = (ExcelPanel) findViewById(R.id.content_container);
        adapter = new CustomAdapter(this, blockListener);
        excelPanel.setAdapter(adapter);
//        excelPanel.setOnLoadMoreListener(this);
        excelPanel.addOnScrollListener(onScrollListener);

        for (int i = 0; i < x.length; i++) {
            CourseBean courseBean = new CourseBean(x[i], y[i], subject[i], teacherNAME[i]);
            courseBeans.add(courseBean);
        }

        initData();
    }

    private ExcelPanel.OnScrollListener onScrollListener = new ExcelPanel.OnScrollListener() {
        @Override
        public void onScrolled(ExcelPanel excelPanel, int dx, int dy) {
            super.onScrolled(excelPanel, dx, dy);
            Log.e("acjiji", dx + "     " + dy);
        }
    };

    private View.OnClickListener blockListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Cell cell = (Cell) view.getTag();
            if (cell != null) {
                if (cell.getStatus() == 0) {
                    Toast.makeText(getApplicationContext(), "没有课呦", Toast.LENGTH_SHORT).show();
                } else if (cell.getStatus() == 1) {
                    Toast.makeText(getApplicationContext(), "当前课是：" + cell.getSubject(), Toast.LENGTH_SHORT).show();
                }
            }
        }
    };


    private void initData() {
        rowTitles = new ArrayList<>();
        colTitles = new ArrayList<>();
        cells = new ArrayList<>();
        for (int i = 0; i < ROW_SIZE; i++) {
            cells.add(new ArrayList<Cell>());
        }

        List<RowTitle> rowTitles1 = genRowData();
        List<List<Cell>> cells1 = genCellData();

        rowTitles.addAll(rowTitles1);
        for (int i = 0; i < cells1.size(); i++) {
            cells.get(i).addAll(cells1.get(i));
        }

        if (colTitles.size() == 0) {
            colTitles.addAll(genColData());
        }
        adapter.setAllData(colTitles, rowTitles, cells);
    }


    //====================================模拟生成数据==========================================
    private List<RowTitle> genRowData() {//纵坐标
        List<RowTitle> rowTitles = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < PAGE_SIZE; i++) {
            RowTitle rowTitle = new RowTitle();
            rowTitle.setWeekString(top_Week[i]);
            rowTitles.add(rowTitle);
        }
        return rowTitles;
    }

    private List<ColTitle> genColData() {//横坐标
        List<ColTitle> colTitles = new ArrayList<>();
        for (int i = 0; i < ROW_SIZE; i++) {
            ColTitle colTitle = new ColTitle();
            colTitle.setRoomNumber(left_time[i]);

            colTitles.add(colTitle);
        }
        return colTitles;
    }

    private boolean is_has=false;
    private List<List<Cell>> genCellData() {
        List<List<Cell>> cells = new ArrayList<>();
        for (int i = 0; i < ROW_SIZE; i++) {//x坐标
            List<Cell> cellList = new ArrayList<>();
            cells.add(cellList);
            for (int j = 0; j < PAGE_SIZE; j++) {//y坐标
                Cell cell = new Cell();
                is_has=false;
                for (int k = 0; k < courseBeans.size(); k++) {//这个是看当前x，y轴的数据在json数据中有没有，有就添加

                    if (courseBeans.get(k).getX() == i && courseBeans.get(k).getY() == j) {
                        is_has=true;
                        cell.setStatus(1);
                        cell.setSubject(courseBeans.get(k).getSubject());
                        cell.setTeacherNAME(courseBeans.get(k).getTeacherNAME());
                    }

                }

                if (!is_has)//这个是json数据中没有当前x，y轴的数据
                cell.setStatus(0);

                cellList.add(cell);
            }
        }
        return cells;
    }
}
